package com.xmy.studentmanager.mapper;

import com.xmy.studentmanager.pojo.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @version: V1.0
 * @author: xmy
 * @className: StudentMapper
 * @description:
 * @data: 2019-04-23 13:33
 **/
public interface StudentMapper {

    /**
     * @description: 查询全部学生
     * @auther: XMY
     * @date: 2019-04-23 13:33
     */
    @Select({"<script>",
            "select * from student s,grade g where s.gradeId = g.gradeId",
            "<if test='name!=null'>",
            " and name like CONCAT('%',#{name},'%')",
            "</if>",
            "<if test='gradeId!=null'>",
            " and s.gradeId=#{gradeId}",
            "</if>",
            "</script>"
    })
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "grade",column = "gradeId",one=@One(select = "com.xmy.studentmanager.mapper.GradeMapper.selGradeNameById"))
    })
    List<Student> selAllStudent(String name,Integer gradeId);

    /**
     * @description: 根据id查询学生
     * @auther: XMY
     * @date: 2019-04-23 13:33
     */
    @Select("select * from student where id = #{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "sex",column = "sex"),
            @Result(property = "grade",column = "gradeId",one=@One(select = "com.xmy.studentmanager.mapper.GradeMapper.selGradeNameById"))
    })
    Student selStudentById(Integer id);

    /**
     * @description: 新增学生
     * @auther: XMY
     * @date: 2019-04-23 13:35
     */
    @Insert("INSERT INTO `studentdb`.`student`(`id`, `name`, `sex`, `gradeId`) VALUES (null, #{name}, #{sex}, #{gradeId})")
    int addStudent(Student student);

    /**
     * @description: 修改学生信息
     * @auther: XMY
     * @date: 2019-04-23 13:36
     */
    @Update("UPDATE `studentdb`.`student` SET `name` = #{name}, `sex` = #{sex}, `gradeId` = #{gradeId} WHERE `id` = #{id}")
    int updateStudent(Student student);

    /**
     * @description: 删除学生
     * @auther: XMY
     * @date: 2019-04-23 13:37
     */
    @Delete("delete from student where id = #{id}")
    int delStudent(Integer id);
}
